package zqservlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import zqtools.DBUtil;

import java.io.IOException;

@WebServlet(name = "loginServlet", urlPatterns = "/login")
public class loginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String as_opid = request.getParameter("name");
        String as_pass = request.getParameter("pass");

        O_INFO of = new O_INFO();
        of.setCMD("ZA");
        of.setVALUE01(as_opid);
        of.setVALUE02(as_pass);

        JSONObject j = DBUtil.f_query2(of);
        JSONObject jobj1 = j.getJSONArray("db1").getJSONObject(0);

        if (("ZZ00").equals(jobj1.get("VALUE01").toString())) {
            JSONArray jobj2 = j.getJSONArray("db2");
            JSONArray jobj3 = j.getJSONArray("db2");
            StringBuilder ls_nav = new StringBuilder();
            if (jobj2 != null) {
                ls_nav.append("<div class='menu_dropdown bk_2' style='font-size:16px; font-family: \"Microsoft Yahei\", serif;'><dl>");
                for (int i = 0; i < jobj2.size(); i++) {
                    JSONObject o2 = jobj2.getJSONObject(i);
                    String ls_node = o2.get("VALUE01").toString();
                    if ((Integer.parseInt(ls_node) % 100) == 0) {
                        ls_nav.append("<dt style='display:block'><i class='Hui-iconfont'>").append(o2.get("VALUE06").toString()).append("</i>&nbsp;&nbsp;").append(o2.get("VALUE03").toString()).append("<i class='Hui-iconfont menu_dropdown-arrow'>&#xe6d5;</i></dt><dd><ul>");

                        for (int k = 0; k < jobj3.size(); k++) {
                            JSONObject o3 = jobj3.getJSONObject(k);
                            String ls_pnode = o3.get("VALUE02").toString();
                            String ls_href = o3.get("VALUE04").toString().replaceAll("XXX", as_opid);
                            if (ls_pnode.equals(ls_node)) {
                                ls_nav.append("<li style='display:").append(o3.get("VALUE07").toString()).append("'><a data-href='").append(ls_href).append("' data-title='").append(o3.get("VALUE03").toString()).append("'>").append(o3.get("VALUE03").toString()).append("</a></li>");
                            }
                        }
                        ls_nav.append("</ul></dd>");
                    }
                }
                ls_nav.append("</dl></div>");
            }

            request.setAttribute("db1", jobj1);
            request.setAttribute("nav", ls_nav.toString());

            HttpSession session = request.getSession();

            session.setAttribute("sessionOpid", jobj1.get("VALUE03"));
            session.setAttribute("sessionOpmm", jobj1.get("VALUE04"));

            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } else if (("ZZ01").equals(jobj1.get("VALUE01").toString())) {
            request.setAttribute("info", jobj1.get("VALUE02").toString());
            request.getRequestDispatcher("/w_login.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/w_login.jsp").forward(request, response);
    }

}
